﻿using System;
using System.Collections.Generic;

using System.Text;
using EasyHook;
using System.Runtime.Remoting;
using System.Windows.Forms;

using System.Diagnostics;
using System.Drawing.Win32;
using System.Drawing;
using Layers;
using HoldemGame;

namespace HoldemHook
{
    public class FTPlayer
    {
        public string ID;
        public double Stake;
    }





    public class FTDriver  
    {
        static string ChannelName = null;
        static string TargetName = "FullTiltPoker";
        static string AppName = "InjectProxy.dll";

        static private EventHandler<DriverEventArgs> _drvEvent;

        static public WinWndMap WndMap = new WinWndMap();

        static public GUINodeMap GuiNodes = new GUINodeMap();
    

        static public event EventHandler<DriverEventArgs> DriverEvent
        {
            add { _drvEvent += value; }
            remove { _drvEvent -= value; }
        }



        static Dictionary<string, WinWnd> Games = new Dictionary<string, WinWnd>();

        public static void Start()
        {

            InjectClient.Inject(TargetName, AppName);

            //InjectClient.InjectEvent += new EventHandler<CallEventArgs>(Instance_InjectEvent);
            InjectClient.WindowEvent += new EventHandler<WindowEventArgs>(Instance_WindowEvent);

            InjectClient.GUINodeEvent += new EventHandler<GUINodeEventArgs>(Instance_GUINodeEvent);

            InjectClient.InjectEvent += new EventHandler<CallEventArgs>(Instance_CallEvent);

        }

        static void Instance_CallEvent(object sender, CallEventArgs e)
        {
            if (e.Function.StartsWith("QPainter::drawtextItem"))
            {
                MyLogger.WriteLine("!{3}!QPainter::drawTextItem(X={0},Y={1},\'{2}\'", e.Args[1], e.Args[2], e.Args[0], e.InTime);
            }
        }

        static void Instance_WindowEvent(object sender, WindowEventArgs e)
        {
            WinWnd wnd = null;
            if (e.Wnd.ClassName.Equals("QWidget") && e.Wnd.Text.IndexOf("Hold'em") >= 0)
            {
                Console.WriteLine("FOUND: FTTableWidget TITLE=" + e.Wnd.Text);

                wnd = new QtWidget(e.Wnd);
                wnd.ListenCalls();
                NotifyGameOpened(wnd);
            }else
            {
                Console.WriteLine("Unknown window CLASS=" + e.Wnd.ClassName + ", TITLE=" + e.Wnd.Text);
                wnd = new WinWnd(e.Wnd);
            }
            if (null != wnd)
                WndMap.Add(wnd);
        }

        static void Instance_GUINodeEvent(object sender, GUINodeEventArgs e)
        {
            GuiNodes.Add(e.Node);
        }

        static public void NotifyGameOpened(WinWnd table)
        {
            if(null!=_drvEvent)
                _drvEvent(table, new DriverEventArgs(DriverEventArgs.EventKind.TableConnected, table));                
        }

        static void FTChat_NewMessage(object sender, TextOutEventArgs e)
        {
            Console.WriteLine("\tFullTiltChat[{0}]: {1}",e.InTime, e.Message);
        } 
        
        static void Instance_InjectEvent(object sender, CallEventArgs e)
        {
            
        }
    }
}
